﻿Imports System.Windows.Forms

Public Class ResourceDialog

    Private Sub ResourceDialog_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CenterToScreen()

        SearchResults.SelectedIndex = 0
    End Sub

    Private Sub ResourceDialog_Shown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown
        ' Dirty hack to default select the search box on opening
        SelectNextControl(SearchLabel, True, True, False, False)
    End Sub

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        If InSearchResultItemRange(SearchResults.SelectedIndex) Then
            Me.DialogResult = System.Windows.Forms.DialogResult.OK
            _selectedfullname = Globals.CurrentProject.DevelopmentFolder + CType(SearchResults.Items(SearchResults.SelectedIndex), String)
            Me.Close()
        Else
            ' This should never happen, but if it does...
            Throw New ArgumentOutOfRangeException("No Filename selected or Filename selected is not in range")
        End If
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    ' Filter the search results according to the SearchBox
    Private Sub SearchBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SearchBox.TextChanged
        SearchResults.Items.Clear()
        If SearchBox.Text = "" Then
            SearchResults.Items.AddRange(_files.ToArray())
        Else
            For i As Integer = 0 To _files.Count - 1
                If _files(i).ToLower().IndexOf(SearchBox.Text.ToLower()) >= 0 Then
                    SearchResults.Items.Add(_files(i))
                End If
            Next
        End If

        ' Select the first index if there are items
        If SearchResults.Items.Count > 0 Then
            SearchResults.SelectedIndex = 0
        End If

        OK_Button.Enabled = InSearchResultItemRange(SearchResults.SelectedIndex)
    End Sub

    Private Sub SearchResults_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SearchResults.SelectedIndexChanged
        OK_Button.Enabled = InSearchResultItemRange(SearchResults.SelectedIndex)
    End Sub

    Private Sub SearchResults_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles SearchResults.MouseDoubleClick
        If InSearchResultItemRange(SearchResults.SelectedIndex) Then
            Me.DialogResult = System.Windows.Forms.DialogResult.OK
            _selectedfullname = Globals.CurrentProject.DevelopmentFolder + CType(SearchResults.Items(SearchResults.SelectedIndex), String)
            Me.Close()
        End If
    End Sub

    Private _files As List(Of String)
    WriteOnly Property Files As List(Of String)
        Set(ByVal value As List(Of String))
            _files = value
            SearchResults.Items.AddRange(value.ToArray())
        End Set
    End Property

    Private _selectedfullname As String = ""
    ReadOnly Property SelectedFullName As String
        Get
            Return _selectedfullname
        End Get
    End Property

    Private Function InSearchResultItemRange(ByVal index As Integer) As Boolean
        If index >= 0 And
            index < SearchResults.Items.Count Then
            Return True
        End If

        Return False
    End Function

    Private Sub SearchBox_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles SearchBox.KeyDown
        Dim newindex As Integer = SearchResults.SelectedIndex
        If e.KeyCode = Keys.Up Then
            newindex -= 1
        ElseIf e.KeyCode = Keys.Down Then
            newindex += 1
        ElseIf e.KeyCode = Keys.PageUp Then
            newindex -= SearchResults.ItemHeight
        ElseIf e.KeyCode = Keys.PageDown Then
            newindex += SearchResults.ItemHeight
            If Not InSearchResultItemRange(newindex) Then
                newindex = SearchResults.Items.Count - 1
            End If
        ElseIf e.KeyCode = Keys.Home Then
            newindex = 0
        ElseIf e.KeyCode = Keys.End Then
            newindex = SearchResults.Items.Count - 1
        End If

        If InSearchResultItemRange(newindex) Then
            SearchResults.SelectedIndex = newindex
        End If

    End Sub

End Class
